package appapi.controller.oider;

import appapi.Global.BusinessException;
import appapi.Request.OrganizaPageReq;
import appapi.Request.ProjPageReq;
import appapi.Request.ProjReq;
import appapi.common.ResponseBean;
import appapi.config.CurrentUser;
import appapi.entity.Zhongjiao.OrganizationDto;
import appapi.entity.Zhongjiao.ProjectDto;
import appapi.entity.basic.UserInfo;
import appapi.model.ResponseDataModel;
import appapi.model.ResponseModel;
import appapi.service.IOrganization;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author djq
 * @date 2023/5/23 10:49
 * @description:
 */
@Api(tags = "组织机构")
@RestController
@RequestMapping(value = "/api/organization")
public class OrganizationController {
    @Resource
    private IOrganization iOrganization;

    //region 组织机构
    /**
     * 组织机构的分页查询
     *
     * @return
     * @auther djq 2023/5/23
     */
    @ApiOperation(value = "组织机构的分页查询", notes = "组织机构的分页查询")
    @GetMapping(value = "/GetOrganizationList") 
    public ResponseBean GetOrganizationList(@Valid OrganizaPageReq projReq) {
        try {
            if (projReq.getCurrPage() == null || projReq.getPageSize() == null) {
                projReq.setCurrPage(1);
                projReq.setPageSize(10);
            }

            List<Map<String, Object>> projects = iOrganization.SelectOrganizationPage(projReq);

            PageInfo pageInfo = new PageInfo(projects, projReq.getPageSize());

            return ResponseBean.success(pageInfo);
        } catch (Exception e) {
            return ResponseBean.fail("组织机构的分页异常");
        }
    }
    
    /**
     * 获取组织机构详情
     *
     * @return
     * @auther 2023/5/12
     */
    @ApiOperation(value = "获取组织机构详情", notes = "获取组织机构详情")
    @GetMapping(value = "/GetOrganizationById")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "orgaId", dataType = "long", value = "组织机构Id")
    })
    public ResponseBean GetOrganizationById(Long orgaId) {
        try {
            Map<String, Object> map = iOrganization.SelectOrgDetail(orgaId);

            return ResponseBean.success(map);
        } catch (BusinessException exception) {
            return ResponseBean.fail("获取组织机构详情异常");
        }
    }

    /**
     * 添加组织机构
     *
     * @return
     * @auther 2023/5/12
     */
    @ApiOperation(value = "添加组织机构", notes = "添加组织机构")
    @PostMapping(value = "/AddOrganization")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "dto", dataType = "object", value = "组织机构参数")
    })
    public ResponseBean AddOrganization(@Validated @RequestBody OrganizationDto dto) {
        try {
            iOrganization.InsertOrganization(dto);
            return ResponseBean.success();
        } catch (BusinessException exception) {
            return ResponseBean.fail("添加组织机构异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 编辑组织机构
     *
     * @return
     * @auther  2023/5/23
     */
    @ApiOperation(value = "编辑组织机构", notes = "编辑组织机构")
    @PostMapping(value = "/EditOrganization")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "formData", name = "companyDto", dataType = "object", value = "组织机构参数")
    })
    public ResponseBean EditOrganization(@Validated @RequestBody OrganizationDto dto) {

        try {
            iOrganization.UpdateOrganization(dto);
            return ResponseBean.success();
        } catch (BusinessException exception) {
            return ResponseBean.fail("编辑组织机构异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 删除组织机构
     *
     * @return
     * @auther lc 2023/5/23
     */
    @ApiOperation(value = "删除组织机构", notes = "删除组织机构")
    @PostMapping(value = "/DeleteOrganization")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "orgaId", dataType = "Long", value = "组织机构id")
    })
    public ResponseBean DeleteOrganization(Long orgaId) {
        try {
            iOrganization.DelOrganization(orgaId);
            return ResponseBean.success();
        } catch (BusinessException exception) {
            return ResponseBean.fail("删除组织机构异常: ".concat(exception.getMessage()));
        }
    }

    /**
     * 组织机构树查询
     *
     * @return
     * @auther djq 2023/5/23
     */
    @ApiOperation(value = "组织机构树查询", notes = "组织机构树查询")
    @GetMapping(value = "/GetOrganizationTree")
    public ResponseBean GetOrganizationTree(@CurrentUser UserInfo user,Long orgaPid) {
        try {
            List<Map<String, Object>> tree = iOrganization.SelectOrgTree(user,orgaPid);

            return ResponseBean.success(tree);
        } catch (Exception e) {
            return ResponseBean.fail("组织机构树查询异常");
        }
    }

    //endregion
}
